package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.entity.CvdGuidePosEntity;
import com.hitqz.robot.api.business.dto.CvdGuidePosQueryDTO;
import com.hitqz.robot.api.business.dto.CvdGuidePosSaveDTO;
import com.hitqz.robot.api.business.dto.CvdGuidePosUpdateDTO;
import com.hitqz.robot.api.business.vo.CvdGuidePosVO;
import com.hitqz.robot.biz.mapper.CvdGuidePosMapper;
import com.hitqz.robot.biz.service.CvdGuidePosService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Optional;

/**
 * 迎宾讲解位置
 *
 * @author kehanjiang
 * @date 2025-09-25 09:53:39
 */
@Service
@RequiredArgsConstructor
public class CvdGuidePosServiceImpl extends ServiceImpl<CvdGuidePosMapper, CvdGuidePosEntity> implements CvdGuidePosService {

	private CvdGuidePosVO convertToVO(Object o) {
        if (o instanceof CvdGuidePosEntity) {
            CvdGuidePosVO vo = new CvdGuidePosVO();
            BeanUtil.copyProperties(o, vo);
			//设置其他参数
			
            return vo;
        }
        return null;
    }
	
	private void preCheck(CvdGuidePosEntity entity, Boolean update) {
        Long id = entity.getId();
        if (update) {//更新操作
           Assert.notNull(id, "id不可为空！");
           boolean exist = this.exists(Wrappers.<CvdGuidePosEntity>lambdaQuery()
                   .eq(CvdGuidePosEntity::getCode, entity.getCode())
                   .ne(CvdGuidePosEntity::getId, id));
           Assert.isTrue(!exist, "该地图点位已存在！");
        } else {
            boolean exist = this.exists(Wrappers.<CvdGuidePosEntity>lambdaQuery()
                    .eq(CvdGuidePosEntity::getCode, entity.getCode()));
            Assert.isTrue(!exist, "该地图点位已存在！");
        }
    }

    @Override
    public Page<CvdGuidePosVO> getCvdGuidePosPage(Page page, CvdGuidePosQueryDTO dto){
        CvdGuidePosEntity entity = new CvdGuidePosEntity();
        BeanUtils.copyProperties(dto, entity);
        //entity.setName(null);
        //final String name = dto.getName();
        LambdaQueryWrapper<CvdGuidePosEntity> wrapper = new LambdaQueryWrapper<>(entity);
        //wrapper.like(StringUtils.hasText(name), CvdGuidePosEntity::getName, name);
        wrapper.orderByDesc(CvdGuidePosEntity::getId);
        return (Page<CvdGuidePosVO>) this.page(page, wrapper)
                .convert(this::convertToVO);
    }
	
    @Override
    public CvdGuidePosVO getCvdGuidePosById(Long id){
        Assert.notNull(id, "id不可为空！");
        return convertToVO(this.getById(id));
    }
	
	
    @Override
    public Boolean saveCvdGuidePos(CvdGuidePosSaveDTO dto){
        CvdGuidePosEntity entity = new CvdGuidePosEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, false);
        return this.save(entity);
    }
	
    @Override
    public Boolean updateCvdGuidePosById(CvdGuidePosUpdateDTO dto){
        CvdGuidePosEntity entity = new CvdGuidePosEntity();
        BeanUtils.copyProperties(dto, entity);
        this.preCheck(entity, true);
        return this.updateById(entity);
    }
	
    @Override
    public Boolean removeCvdGuidePosBatchByIds(List<Long> ids){
        Assert.notEmpty(ids, "ID不可为空！");
        return this.removeBatchByIds(ids);
    }
}